package com.nsoft.thickclient.trackMove.user;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.persistence.NoResultException;
import javax.swing.Box;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.nsoft.server.master.beans.UserMaster;
import com.nsoft.server.serviceImpl.FacadeFactoryImpl;
import com.nsoft.thickclient.beanUtils.ActionDescriptor;
import com.nsoft.thickclient.beanUtils.NButton;
import com.nsoft.thickclient.components.VOPropertyPasswordField;
import com.nsoft.thickclient.components.VOPropertyTextField;
import com.nsoft.thickclient.containers.VoPropertyFieldPanel;
import com.nsoft.thickclient.controller.ApplicationController;
import com.nsoft.thickclient.model.VoModel;
import com.nsoft.thickclient.trackMove.intr.user.User;
import com.nsoft.thickclient.utils.IconFactory;

public class LoginDialog extends JDialog {

	/**
	 * serialVersionUID = 1L.
	 */
	private static final long serialVersionUID = 1L;

	private ActionDescriptor login = new ActionDescriptor("Login", "doLogin",
			this);

	private ActionDescriptor cancel = new ActionDescriptor("Cancel",
			"doCancel", this);

	private User user = null;

	VoModel model = new VoModel(User.class);

	public LoginDialog(ApplicationController controller) {
		super(controller.getFrame());
		JPanel contentPanel = new JPanel(new BorderLayout());
		VoPropertyFieldPanel fieldPanel = new VoPropertyFieldPanel();
		fieldPanel.setRightAlignment(GridBagConstraints.WEST);
		fieldPanel.setInsets(new Insets(5, 5, 5, 5));
		fieldPanel
				.addProperty(
						new JLabel(IconFactory.getSecurityIcon()),
						"<html><p><font face='Arial' size='5' color='#FF0000'>Security check!</font></p></html>");
		fieldPanel
				.addProperty("Login", new VOPropertyTextField(model, "login"));
		fieldPanel.addProperty("Password", new VOPropertyPasswordField(model,
				"password"));
		contentPanel.add(fieldPanel, BorderLayout.CENTER);
		contentPanel.add(getButtonPanel(), BorderLayout.PAGE_END);
		setContentPane(contentPanel);
		setTitle("Login");
		setModal(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(350, 200);
		controller.centerToScreen(this);

	}

	private JPanel getButtonPanel() {
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		buttonPanel.add(new NButton(login));
		buttonPanel.add(Box.createHorizontalStrut(50));
		buttonPanel.add(new NButton(cancel));
		return buttonPanel;
	}

	public void doLogin() {
		user = (User) model.getValueObject();
		UserMaster master = null;
		try {
			master = FacadeFactoryImpl.getUserService().getbyNamePass(user);
		} catch (NoResultException exception) {
			exception.printStackTrace();
		}
		if (master != null) {
			close();
		}
	}

	public void doCancel() {
		close();
		// System.exit(0);
	}

	public User login() {
		setVisible(true);
		return user;
	}

	private void close() {
		setVisible(false);
		dispose();
	}
}
